package com.softline.documentsmall.service.impl;

import com.softline.documentsmall.dao.DictionaryInfoMapper;
import com.softline.documentsmall.dao.DictionaryKeywordConfigMapper;
import com.softline.documentsmall.dao.DictionaryMapper;
import com.softline.documentsmall.entity.Dictionary;
import com.softline.documentsmall.entity.DictionaryInfoVO;
import com.softline.documentsmall.entity.DictionaryVO;
import com.softline.documentsmall.service.TooLanuageSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wph
 * @Date 2020/12/29 14:40
 */
@Service
@Slf4j
public class TooLanuageSearchServiceImpl implements TooLanuageSearchService {
    @Autowired
    DictionaryKeywordConfigMapper dictionaryKeywordConfigMapper;
    @Autowired
    DictionaryMapper dictionaryMapper;
    @Autowired
    DictionaryInfoMapper dictionaryInfoMapper;

    /**
     * 多语种术语搜索,根据用户输入语言，进行模糊搜索，
     *
     * @return
     * @author wph
     */
    @Override
    public List<DictionaryVO> tooLanguageSearch(Integer pageNo, Integer pageSize, String type, String name) {
        List<Long> byKeyword = dictionaryKeywordConfigMapper.findByKeyword((pageNo - 1) * pageSize, pageSize, name);
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        if (!StringUtils.isEmpty(byKeyword)) {
            List<Dictionary> dictionaryList = dictionaryMapper.findByListId(byKeyword);
            if (!StringUtils.isEmpty(dictionaryList)) {
                switch (type) {
                    case "en":
                        dictionaryVOList = convertEn(dictionaryList);
                        break;
                    case "cn":
                        dictionaryVOList = convertCn(dictionaryList);
                        break;
                    case "ar":
                        dictionaryVOList = convertAr(dictionaryList);
                        break;
                    case "fr":
                        dictionaryVOList = convertFr(dictionaryList);
                        break;
                    case "de":
                        dictionaryVOList = convertDe(dictionaryList);
                        break;
                    case "it":
                        dictionaryVOList = convertIt(dictionaryList);
                        break;
                    case "jp":
                        dictionaryVOList = convertJp(dictionaryList);
                        break;
                    case "kr":
                        dictionaryVOList = convertKr(dictionaryList);
                        break;
                    case "pt":
                        dictionaryVOList = convertPt(dictionaryList);
                        break;
                    case "ru":
                        dictionaryVOList = convertRu(dictionaryList);
                        break;
                    case "es":
                        dictionaryVOList = convertEs(dictionaryList);
                        break;
                }
            }
        }
        return dictionaryVOList;
    }

    /**
     * 根据用户输入关键词,语种搜索论文、专利等信息进行列表展示
     *
     * @return
     * @author wph
     */
    @Override
    public List<DictionaryInfoVO> infoSearch(Integer pageNo, Integer pageSize, String type, String name) {
        List<Long> byKeyword = dictionaryKeywordConfigMapper.findByKeyword((pageNo - 1) * pageSize, pageSize, name);
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        if (!StringUtils.isEmpty(byKeyword)) {
            return dictionaryInfoMapper.findByListId(byKeyword);
        }
        return null;
    }

    private List<DictionaryVO> convertEs(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameEs(dictionary.getNameEs());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertRu(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameRu(dictionary.getNameRu());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertPt(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNamePt(dictionary.getNamePt());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertKr(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameKr(dictionary.getNameKr());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertJp(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameJp(dictionary.getNameJp());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertIt(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameIt(dictionary.getNameIt());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertDe(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameDe(dictionary.getNameDe());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertFr(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameFr(dictionary.getNameFr());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertAr(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameAr(dictionary.getNameAr());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertCn(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameCn(dictionary.getNameCn());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }

    private List<DictionaryVO> convertEn(List<Dictionary> dictionaryList) {
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        for (int i = 0; i < dictionaryList.size(); i++) {
            DictionaryVO dictionaryVO = new DictionaryVO();
            Dictionary dictionary = dictionaryList.get(i);
            dictionaryVO.setNameEn(dictionary.getNameEn());
            dictionaryVO.setId(dictionary.getId());
            dictionaryVOList.add(dictionaryVO);
        }
        return dictionaryVOList;
    }
}
